#region Copyright (c) 2000-2011 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2011 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2010 Developer Express Inc.

namespace DevExpress.OAuth {
    using System;

    public struct Nonce {
        static Random s_random;

        /// <summary>
        /// OAuth: oauth_nonce
        /// The Consumer SHALL then generate a Nonce value that is unique for all requests with that timestamp. 
        /// A nonce is a random string, uniquely generated for each request. The nonce allows the Service Provider to verify that a 
        /// request has never been made before and helps prevent replay attacks when requests are made over a non-secure channel (such as HTTP).
        /// </summary>
        public static Nonce CreateNonce() {
            if (s_random == null) {
                s_random = new Random();
            }
            return new Nonce(
                (long)(s_random.Next(3833448, 8443383) + DateTime.Now.Ticks));
        }

        public Nonce(long value) {
            if (value <= 0) {
                throw new ArgumentOutOfRangeException("value");
            }
            _value = value.ToString();
        }

        public Nonce(string value) {
            if (String.IsNullOrEmpty(value)) {
                throw new ArgumentNullException("value");
            }
            _value = value;
        }

        public bool IsEmpty {
            get {
                if (String.IsNullOrEmpty(_value)) {
                    return true;
                }
                return false;
            }
        }

        string _value;
        public string Value {
            get {
                if (String.IsNullOrEmpty(_value)) {
                    throw new InvalidOperationException();
                }
                return _value;
            }
        }

    }
}